"""
@Project ：python
@File    ：02-疫情地图-全国疫情地图.py
@IDE     ：PyCharm
@Author  ：DongCha
@Date    ：2024/4/7 007 19:34
"""

# 数据结构
# {
#     "lastUpdateTime": "2021-08-18 10:53:30",
#     "chinaTotal": {
#         "confirm": 122482,
#         "heal": 112984,
#         "dead": 5669
#     },
#     "chinaAdd": {
#         "confirm": 122482,
#         "heal": 112984,
#         "dead": 5669
#     },
#     "isShowAdd": true,
#     "showAddSwitch": {
#         "all": true,
#         "confirm": true,
#         "suspect": true
#     },
#     "areaTree": [
#         {
#             "name": "中国",
#             "today": {
#                 "confirm": 49,
#                 "isUpdated": true
#             },
#             "total": {
#                 "nowConfirm": 3829,
#                 "confirm": 122482,
#                 "suspect": 1
#             },
#             "children": [
#                 {
#                     "name": "台湾",
#                     "today": {
#                         "confirm": 18,
#                         "confirmCuts": 0,
#                         "isUpdated": true,
#                         "tip": "",
#                         "wzz_add": 0
#                     },
#                     "total": {
#                         "nowConfirm": 1864,
#                         "confirm": 15880,
#                         "suspect": 0,
#                         "dead": 821,
#                         "deadRate": "5.17",
#                         "showRate": false,
#                         "heal": 13195,
#                         "healRate": "83.09",
#                         "showHeal": true,
#                         "wzz": 0
#                     },
#                     "children": [
#                         {
#                             "name": "地区待确认",
#                             "today": {
#                                 "confirm": 18,
#                                 "confirmCuts": 0,
#                                 "isUpdated": true
#                             },
#                             "total": {
#                                 "nowConfirm": 1864,
#                                 "confirm": 15880,
#                                 "suspect": 0,
#                                 "dead": 821,
#                                 "deadRate": "5.17",
#                                 "showRate": false,
#                                 "heal": 13195,
#                                 "healRate": "83.09",
#                                 "showHeal": true,
#                                 "wzz": 0
#                             }
#                         }
#                     ]
#                 },
#                 {
#                     ...
#                 },
#                 ...
#             ]
#         }
#     ]
# }

import json

from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts, TitleOpts, InitOpts

# 存储地图数据
map_data = []


# 返回带有省的方法
def province(pro):
    provinces = ["北京市", "天津市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "上海市",
                 "江苏省", "浙江省", "安徽省", "福建省", "江西省",
                 "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省", "重庆市", "四川省",
                 "贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省",
                 "青海省", "宁夏回族自治区", "新疆维吾尔自治区", "台湾省", "香港特别行政区", "澳门特别行政区"]

    # for p in provinces:
    #     if p.find(pro) >= 0:
    #         return p
    #         break

    for p in provinces:
        if pro in p:
            return p
            break


# 读取数据
with open("./数据/疫情.txt", "r", encoding="utf-8") as f:
    data = f.read()

# 获取各省数据
data_diet = json.loads(data)
all_data = data_diet["areaTree"][0]["children"]
for area in all_data:
    province_name = province(area["name"])
    data_tuple = (province_name, area["total"]["confirm"])
    map_data.append(data_tuple)

# 获取图表对象
my_map = Map(init_opts=InitOpts(width="700px", height="700px"))

# 传入图表数据
my_map.add("人数", map_data, "china")

# 设置属性
my_map.set_global_opts(
    title_opts=TitleOpts(title="2020年疫各省情确诊人数"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,  # 开启手动校准范围
        pieces=[  # 设置具体范围
            {"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
            {"min": 10, "max": 99, "label": "10-99人", "color": "#FFFF99"},
            {"min": 100, "max": 999, "label": "100-999人", "color": "#FF9966"},
            {"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#FF6666"},
            {"min": 10000, "label": "1万人及以上", "color": "#990033"}
        ]
    )
)

# 绘制地图
my_map.render("./maps/my_map.html")
